package com.ruoyi.init;

import com.abth.douyin.domain.DouyinCity;
import com.abth.douyin.service.IDouyinCityService;
import com.abth.douyin.util.SpiderResult;
import com.abth.douyin.util.SpiderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class InitExector implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(InitExector.class);

    @Autowired
    private SpiderUtil spiderUtil;

    @Autowired
    private IDouyinCityService douyinCityService;

    /**
     * 采集1：
     * cityMap.put("深圳市", "440300");
     *                 cityMap.put("杭州市", "330100");
     *                 cityMap.put("上海市", "310000");
     *                 cityMap.put("厦门市", "350200");
     * 采集2：
     * cityMap.put("山西省", "140000");
     *                 cityMap.put("安徽省", "340000");
     *                 cityMap.put("江苏省", "320000");
     *                 cityMap.put("福建省", "350000");
     *                 cityMap.put("海南省", "460000");
     *                 cityMap.put("浙江省", "330000");
     *                 cityMap.put("江西省", "360000");
     *                 cityMap.put("上海市", "310000");
     * 采集3：
     *  全量
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        new Thread(()->{
            try {
                DouyinCity query = new DouyinCity();
                query.setStat(1);// 获取未抓取的城市
                List<DouyinCity> douyinCities = douyinCityService.selectDouyinCityList(query);
//                String[] keywords = {"药", "药店", "药房", "中医馆", "中药", "西药", "药店诊所", "药铺", "药材", "药膳"};
//                String[] keywords = {"眼镜", "视光", "近视", "视力", "眼科", "斜视", "弱视"};
                String[] keywords = {"养发", "美发", "理发", "白发", "固发", "脱发", "生发"};
                 for (DouyinCity douyinCity : douyinCities) {
                     douyinCity.setStat(2);// 抓取中
                     douyinCityService.updateDouyinCity(douyinCity);
                     try{
                         for (String keyword : keywords) {
                             log.info("===================开始抓取{}-{}:{}============", douyinCity.getPrivinceName(), douyinCity.getCityName(), keyword);
//                             SpiderResult result = spiderUtil.crawShop(keyword, douyinCity.getCityCode(), "051801");
//                             SpiderResult result = spiderUtil.crawShop(keyword, douyinCity.getCityCode(), "051903");
                             SpiderResult result = spiderUtil.crawShop(keyword, douyinCity.getCityCode(), "170302");
                             // 抓取异常，终止
                             if(result.getLevel() == -1){
                                 log.error(result.getMsg());
                                 return;
                             }
                             log.info("==================={}-{}:{}抓取完成============", douyinCity.getPrivinceName(), douyinCity.getCityName(), keyword);
                         }
                         //  抓取成功；
                         douyinCity.setStat(3);
                         douyinCityService.updateDouyinCity(douyinCity);
                     }catch (Exception e){
                         // 抓取失败
                         douyinCity.setStat(4);
                         douyinCityService.updateDouyinCity(douyinCity);
                     }
                }
            } catch (Throwable e) {
                log.error("异常退出", e);
                throw new RuntimeException(e);
            }
        }).start();

    }
}
